package com.dlc.shop.sys.platform.controller;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.bean.SysConfig;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 系统配置信息
 * @author lgh
 */
@Tag(name = "系统配置信息")
@RestController
@RequestMapping("/sys/config")
@RequiredArgsConstructor
public class SysConfigController{
    private final SysConfigService sysConfigService;

    @Value("${yami.expose.permission:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "所有配置列表" , description = "所有配置列表")
    @Parameter(name = "paramKey", description = "参数名" )
    @PreAuthorize("@pms.hasPermission('sys:config:page')")
    public ServerResponseEntity<IPage<SysConfig>> page(String paramKey,PageParam<SysConfig> page){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.success();
        }
        IPage<SysConfig> sysConfigs = sysConfigService.page(page, new LambdaQueryWrapper<SysConfig>().like(StrUtil.isNotBlank(paramKey),SysConfig::getParamKey,paramKey));
        return ServerResponseEntity.success(sysConfigs);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "配置信息" , description = "配置信息")
    @Parameter(name = "id", description = "配置id" )
    @PreAuthorize("@pms.hasPermission('sys:config:info')")
    public ServerResponseEntity<SysConfig> info(@PathVariable("id") Long id){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.success();
        }
        SysConfig config = sysConfigService.getById(id);
        return ServerResponseEntity.success(config);
    }

    @SysLog("保存配置")
    @PostMapping
    @Operation(summary = "保存配置" , description = "保存配置")
    @PreAuthorize("@pms.hasPermission('sys:config:save')")
    public ServerResponseEntity<Void> save(@RequestBody @Valid SysConfig config){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        if (sysConfigService.count(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, config.getParamKey())) > 0) {
            // 系统已存在相同配置的key
            throw new YamiShopBindException("yami.same.key");
        }
        sysConfigService.save(config);
        // 清除系统配置的缓存
        sysConfigService.removeSysConfig(config.getParamKey());
        return ServerResponseEntity.success();
    }

    @SysLog("修改配置")
    @PutMapping
    @Operation(summary = "修改配置" , description = "修改配置")
    @PreAuthorize("@pms.hasPermission('sys:config:update')")
    public ServerResponseEntity<Void> update(@RequestBody @Valid SysConfig config){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }

        SysConfig dbSysConfig = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, config.getParamKey()));

        if (dbSysConfig != null && !Objects.equals(dbSysConfig.getParamKey(),config.getParamKey())) {
            // 系统已存在相同配置的key
            throw new YamiShopBindException("yami.same.key");
        }

        sysConfigService.updateById(config);
        // 清除系统配置的缓存
        sysConfigService.removeSysConfig(config.getParamKey());
        return ServerResponseEntity.success();
    }

    @SysLog("删除配置")
    @DeleteMapping
    @Operation(summary = "删除配置" , description = "删除配置")
    @Parameter(name = "configIds", description = "配置id列表" )
    @PreAuthorize("@pms.hasPermission('sys:config:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody Long[] configIds){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        if (ArrayUtil.isEmpty(configIds)) {
            return ServerResponseEntity.success();
        }
        List<SysConfig> list = sysConfigService.list(new LambdaQueryWrapper<SysConfig>().in(SysConfig::getId, configIds));
        // 清除系统配置的缓存
        sysConfigService.deleteBatch(configIds);
        for (SysConfig sysConfig : list) {
            sysConfigService.removeSysConfig(sysConfig.getParamKey());
        }
        return ServerResponseEntity.success();
    }

}
